package com.hamilton.erik.pt.mobile.android.widget;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hamilton.erik.pt.core.model.MedicalRecord;
import com.hamilton.erik.pt.mobile.android.R;

public class MedicalRecordListAdapter extends BaseAdapter {

	private List<MedicalRecord> medicalRecords;
	private LayoutInflater layoutInflater;
	
	public MedicalRecordListAdapter(Context context, List<MedicalRecord> medicalRecords) {
		this.medicalRecords = medicalRecords;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return medicalRecords.size();
	}

	@Override
	public Object getItem(int index) {
		return medicalRecords.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MedicalRecordHolder medicalRecordHolder;
		
		if(convertView == null) {
			convertView = layoutInflater.inflate(R.layout.medical_record_row_view, null);
			
			medicalRecordHolder = new MedicalRecordHolder();
			
			medicalRecordHolder.time = (TextView)convertView.findViewById(R.id.medical_record_time);
			medicalRecordHolder.action = (TextView)convertView.findViewById(R.id.medical_record_action);
			
			convertView.setTag(medicalRecordHolder);
		}
		else {
			medicalRecordHolder = (MedicalRecordHolder)convertView.getTag();
		}
		
		medicalRecordHolder.time.setText(medicalRecords.get(position).getTimestamp());
		medicalRecordHolder.action.setText(medicalRecords.get(position).getAction().toString());
		
		return convertView;
	}

	static class MedicalRecordHolder {
		TextView action;
		TextView time;
	}

}
